package annotationService.util;

import com.db4o.Db4o;
import com.db4o.ObjectContainer;
import com.db4o.query.Query;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.beanutils.*;

/**
 * class to encode and decode db4o file formats
 * @author Andres Westermann
 *
 */
public class DB4OSimpler {

	public static final int OPERATION_SAVE=0;
	public static final int OPERATION_LOAD=1;
	public static final int OPERATION_UPDATE=2;
	public static final int OPERATION_DELETE=3;
	public static final int OPERATION_QUERY=4;
	public static final int OPERATION_LIST=5;
	public static final int OPERATION_CLEAR=6;
	
	@SuppressWarnings({ "deprecation", "unchecked" })
	public static Object generalOperate(
			String fileName,
			int op_code,
			Object example,
			String keyFieldName){
		Object ret=null;
		ObjectContainer db=Db4o.openFile(fileName);
		Iterator iter=null;
		List list=null;
		Query query=null;
		try{
			switch(op_code){
			case OPERATION_SAVE:
				db.set(example);
				ret=example;
				break;
			case OPERATION_LOAD:
				list=db.get(example);
				if (list!=null && list.size()>0){
					ret=list.get(0);
				}
				break;
			case OPERATION_UPDATE:
				query=db.query();
				query.constrain(example.getClass());
				query.descend(keyFieldName)
					.constrain(BeanUtils.getProperty(example, keyFieldName));
				iter=query.execute().listIterator();
				while(iter.hasNext()){
					db.delete(iter.next());
				}
				db.set(example);
				ret=example;
				break;
			case OPERATION_DELETE:
				query=db.query();
				query.constrain(example.getClass());
				query.descend(keyFieldName)
					.constrain(BeanUtils.getProperty(example, keyFieldName));
				iter=query.execute().listIterator();
				if (iter.hasNext()){
					while(iter.hasNext()){
						db.delete(iter.next());
					}
					ret=true;
				}
				else{
					ret=false;
				}
				break;
			case OPERATION_QUERY:
				query=db.query();
				query.constrain(example.getClass());
				//to search through ArrayListString for annotations
				if(BeanUtils.getProperty(example, keyFieldName).contains("[")){
					String queryString = BeanUtils.getProperty(example, keyFieldName);
					query.descend(keyFieldName).constrain(queryString.subSequence(1, queryString.length()-1)).contains();
				}
				//for title name search
				else{
				query.descend(keyFieldName)
					.constrain(BeanUtils.getProperty(example, keyFieldName)).contains();
				}
				iter=query.execute().listIterator();
				list=new ArrayList();
				while(iter.hasNext()){
						list.add(iter.next());
				}
				if (list.size()>0)
					ret=list;
				break;
			case OPERATION_LIST:
				list=new ArrayList();
				iter=db.query(example.getClass()).listIterator();
				while(iter.hasNext()){
					list.add(iter.next());
				}
				if (list.size()>0)
					ret=list;
				break;
			case OPERATION_CLEAR:
				iter=db.query(example.getClass()).listIterator();
				int deleteCount=0;
				while(iter.hasNext()){
					db.delete(iter.next());
					deleteCount++;
				};
				ret=true;
				break;
			}
			db.commit();
		}
		catch(Exception ex){
			db.rollback();
			ex.printStackTrace();
		}
		finally{
			db.close();
		}
		return ret;
	}
}
